package com.mrjy.integral.bo;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.mrjy.common.entity.HappinessInfo;
import com.mrjy.common.entity.IntegralInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.integral.dao.IHappinessInfoDao;
import com.mrjy.integral.dao.IIntegralInfoDao;
import com.mrjy.integral.dto.IntegralRankDTO;
import com.mrjy.integral.util.HappinessInfoEnum;
import com.mrjy.integral.util.HappinessInfoUtil;
import com.mrjy.integral.util.IntegralEnum;
import com.mrjy.integral.util.IntegralUtil;
import com.mrjy.maintain.dao.IUserInfoDao;

@Service("integralInfoBo")
public class IntegralInfoBo implements IIntegralInfoBo{
	
	@Resource
	private IIntegralInfoDao integralInfoDao;
	@Resource
	private IHappinessInfoDao happinessInfoDao;
	@Resource
	private IUserInfoDao userInfoDao;
	
	@Override
	public Object txInsert(IntegralInfo info) {
		Map<String , Object> map = new HashMap<String, Object>();
		info.setCreateTime(new Timestamp(System.currentTimeMillis()));
		info.setDisableFlag(0);
		
		int count = integralInfoDao.insert(info);
		if(count == 1){
			map.put("err_code", 0);
			map.put("err_msg", "插入成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "插入失败")	;
		}
		return map ;
		
	}
	@Override
	public Object queryById(int id) {
		Map<String, Object> map = new HashMap<String,Object>();
		IntegralInfo info = integralInfoDao.queryById(id);
		if(info != null){
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("info", info);
		}else{
			map.put("err_code", 1);
			map.put("err_msg", "无数据");
		}
		return map;
	}
	@Override
	public Object txUpdateById(IntegralInfo info) {
		Map<String , Object> map = new HashMap<String, Object>();
		int count = integralInfoDao.updateById(info);
		if(count == 1){
			map.put("err_code", 0);
			map.put("err_msg", "更新成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "更新失败")	;
		}
		return map ;
	}
	@Override
	public int deleteByIds(int[] ids) {
		try{
			return integralInfoDao.deleteByIds(ids);
		}catch(DataAccessException  e){
			return 0;
		}
	}
	@Override
	public int updateDisableFlag(IntegralInfo info) {
		return integralInfoDao.updateDisableFlag(info);
	}
	@Override
	public Object queryPageByUserId(IntegralInfo info, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser){
			map.put("err_code", -1);
			map.put("err_msg", "该用户为游客");
			map.put("totalIntegral", 0); //历史总积分(不计兑换扣除)
			map.put("integralBalance", 0); //当前积分
			map.put("rows", null);
			return map;
		}
		int totalIntegral = (int) integralInfoDao.queryToTalIntegralByUserId(info);
		map.put("totalIntegral", totalIntegral); //历史总积分(不计兑换扣除)
		map.put("integralBalance", userInfoDao.queryIntegralBalanceByUserId(info.getUserId())); //当前积分
		if(totalIntegral > 0){
			List<IntegralInfo> list = integralInfoDao.queryPageByUserId(info);
			if(list != null && list.size() > 0){
				map.put("err_code", 200);
				map.put("err_msg", "查询成功");
				map.put("rows", list);
				return map;
			}else {
				map.put("err_code", 201);
				map.put("err_msg", "查询无数据");
				map.put("rows", list);
				return map;
			}
		}else{
			map.put("err_code", 202);
			map.put("err_msg", "查询无数据");
			map.put("rows", null);
			return map;
		}
		
	}
	@Override
	public Object addVideoIntegralByUserId(IntegralInfo info) {
		Map<String , Object> map = new HashMap<String , Object>();
		if(info.getUserId() == null ){
			map.put("err_code", -100);
			map.put("err_msg", "用户id为空");
			return map ;
		}
		if(info.getInfoId() == null ){
			map.put("err_code", -101);
			map.put("err_msg", "视频id");
			return map ;
		}
		//根据用户id和视频id查询其有没有记录
		info.setTypeId(IntegralEnum.WATCH_VIDEO.getTypeId());
		List<IntegralInfo> list = integralInfoDao.queryListByInfo(info);
		if(list == null || list.size() == 0){
			//没有记录的则新增积分
			IntegralInfo integral = IntegralUtil.setIntegral(info.getUserId()
					, IntegralEnum.WATCH_VIDEO.getScore()
					, IntegralEnum.WATCH_VIDEO.getTypeDesc()
					, IntegralEnum.WATCH_VIDEO.getTypeId()
					, info.getInfoId());
			
            map.put("score", integral.getScore());
        }
		
		//根据用户id、视频id和类型id查询有没有记录，如果没有则新增幸福值（爱值）
		Map<String,Object> paramMap = new HashMap<String ,Object>();
		paramMap.put("userId", info.getUserId());
		paramMap.put("typeId", HappinessInfoEnum.WATCH_VIDEO.getTypeId());
		paramMap.put("infoId", info.getInfoId());
		List<HappinessInfo> happinessList = happinessInfoDao.queryListByMap(paramMap);
		if(happinessList == null || happinessList.size() == 0){
			//爱值添加
			HappinessInfo hi = HappinessInfoUtil.setHappinessInfo(info.getUserId()
					, HappinessInfoEnum.WATCH_VIDEO.getLoveValue(), HappinessInfoEnum.WATCH_VIDEO.getTypeDesc()
					, HappinessInfoEnum.WATCH_VIDEO.getTypeId(), "0", info.getInfoId());
			happinessInfoDao.insert(hi);
		}
		
		map.put("err_code", 200);
		map.put("err_msg", "成功执行");
		return map;
	}
	@Override
	public Object queryIntegralRank(IntegralInfo info,HttpSession session) {
		Map<String,Object> map = new HashMap<String ,Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser || info.getUserId().intValue() == 0){
			//游客
			map.put("err_code", -1);
			map.put("err_msg", "当前用户为游客");
			map.put("myRank", null);
			map.put("rankList",null);
			return map;
		}
		//我的排名
		List<IntegralRankDTO> myList = integralInfoDao.queryIntegralRank(info);
		IntegralInfo vo = new IntegralInfo();
		vo.setPage(info.getPage());
		vo.setRows(info.getRows());
		List<IntegralRankDTO> rList = integralInfoDao.queryIntegralRank(vo);
		map.put("err_code", 200);
		map.put("err_msg", "查询成功");
		map.put("myRank", myList.get(0));
		map.put("rankList",rList);
		return map;
	}
	
}
